//
//  HNSNHomePageVC.m
//  ZYManagementSystem
//
//  Created by 星道 on 17/6/14.
//  Copyright © 2017年 乾坤. All rights reserved.
//

#import "HNSNHomePageVC.h"
#import "HNSNHomePageCell.h"
#import "HNSNProductListVCTableViewController.h"
#import "HNSNProductOrderVC.h"
#import "HNSNGetMoneyVC.h"
#import "HNSNHeaderView.h"
#import "HNWithdrawCashHistoryVC.h"
@interface HNSNHomePageVC ()


//本面的静态数据组，dict = @{@"Title":title,@"ImgName"}
@property (nonatomic, strong) NSArray <NSDictionary *>*DataArr;

@property (nonatomic, strong) XDNavigationController *Navi;

@end

@implementation HNSNHomePageVC

/**
 姜老师说消耗流量比消夏资源好接受点，就选择每次都请求下
 */
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    // 静置请求小红点数据
    [self RefreshDataForRedCycle];
}



- (void)viewDidLoad{

    [super viewDidLoad];

    //取消滚动条
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    //添加占位View
    self.tableView.tableFooterView = [UIView new];
    //设置预估高度
    self.tableView.rowHeight = 54;
    //取消灰线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //预注册
    [self.tableView registerNib:[UINib nibWithNibName:[HNSNHomePageCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    //取消预估滚动值
    self.automaticallyAdjustsScrollViewInsets = NO;
    //禁止滚动
    self.tableView.scrollEnabled = NO;
}

#pragma mark - 网络请求
/**
 检测小红点
 */
- (void)RefreshDataForRedCycle
{
    [NetManager Get:LLBaseURL(@"yxhUser.do") Auth:YES Params:@{@"action":@"getIsViewed"} Success:^(id Obj) {

        if ([Obj[@"code"] isEqualToNumber:@1000])
        {
            /** 产品订单 */
            NSString *packageCourse  = [Obj[@"t"][@"packageCourse"] stringValue];

            /** 保存临时的值 */
            NSMutableArray *Arr = [NSMutableArray array];
            /** 将是否有小红点加入数据源 */
            [self.DataArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                // 强转
                NSMutableDictionary *Dict = [obj mutableCopy];
                // 判断 产品订单
                if ([Dict[@"Title"] isEqualToString:@"产品订单"])
                {
                    [Dict setObject:packageCourse forKey:@"isViewed"];
                }

                [Arr addObject:Dict];
            }];

            self.DataArr = Arr ;
            // 通知主线程刷新
            [self.tableView reloadData];

        }
        else
        {
            // 失败：不做处理
        }
    } Fail:^(NSError *Error) {
        // 失败：不做处理
    }];
}




#pragma mark - TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //再加上一行退出Cell
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNSNHomePageCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.Dict = self.DataArr[indexPath.row];
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //产品列表
    if (indexPath.row == 0)
    {
        HNSNProductListVCTableViewController *VC = [[HNSNProductListVCTableViewController alloc] init];
        VC.title = @"产品列表";
        [self.navigationController pushViewController:VC animated:YES];
    }

    //产品订单
    else if (indexPath.row == 1) {
        HNSNProductOrderVC *VC = [[HNSNProductOrderVC alloc] init];
        // 身份标志 SN
        VC.HNSNProductOrderType = HNSNProductOrderSN;
        VC.title = @"产品列表";
        [self.navigationController pushViewController:VC animated:YES];
    }

    //套餐提现
    else if (indexPath.row == 2) {
        HNSNGetMoneyVC *VC = [[HNSNGetMoneyVC alloc] init];
        // 标志身份 SN身份
        VC.HNSNGetMoneyType = HNSNGetMoneyTypeSN;
        VC.title = @"套餐提现";
        [self.navigationController pushViewController:VC animated:YES];
    }

    // 提现记录
    
    else if (indexPath.row == 3) {

        //提现记录
        HNWithdrawCashHistoryVC *VC = [[HNWithdrawCashHistoryVC alloc] init];
        // 身份标示 SN机构
        VC.HNWithdrawCashHistoryType = HNWithdrawCashHistorySN;
        [self.navigationController pushViewController:VC animated:YES];
    }

    else {

    }

}

//区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HNSNHeaderView *HeaderView = [[[NSBundle mainBundle] loadNibNamed:[HNSNHeaderView description] owner:nil options:nil] lastObject];
    HeaderView.HeaderName.text = [NSString stringWithFormat:@"%@(%@)",[AccountTool shareAccountTool].currentAccount.websiteName,[AccountTool shareAccountTool].currentAccount.username];
    return HeaderView;
}

//区高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 150 * KScale;
}

#pragma mark - setter
//静态tableView的数据
- (NSArray<NSDictionary *> *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = @[
                     @{@"Title":@"产品列表",@"ImgName":@"icon_list",@"isViewed":@"0"},
                     @{@"Title":@"产品订单",@"ImgName":@"home_icon_class_default",@"isViewed":@"0"},
                     @{@"Title":@"套餐提现",@"ImgName":@"home_icon_money_default-1",@"isViewed":@"0"},
                     @{@"Title":@"提现记录",@"ImgName":@"home_icon_cash_register_default",@"isViewed":@"0"}
                     ];
    }
    return _DataArr;
}
@end
